
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 7 (SUF) ****************************
************* THIS FILE MERGES THE MODEL ESTIMATES WITH THE VISIT DATA *********
********************************************************************************
********************************************************************************

*Version: 2023 March 29


local date "$date"
cap mkdir `"$output/temp"'
cap mkdir `"$output/log/relax_SUF_7_compute_CIP"'
cap mkdir `"$output/data"'
cap mkdir `"$output/model_estimation/relax_SUF"'
cap mkdir `"$output/model_estimation/relax_SUF/parameters"'

cap log close 
log using `"$output/log/relax_SUF_7_compute_CIP/relax_SUF_7_compute_CIP.smcl"', replace 

di "This run uses code version from 2023/03/29"



****************************************************************
****************************************************************
******************* CIP calculation by Size ********************
****************************************************************
****************************************************************
use  `"$output/temp/relax_SUF_estimation_data_bin.dta"', clear

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



//merge in model estimates 
merge m:1 visit_code_cat state pay_type_pooled size_indicator using `"$output/model_estimation/relax_SUF/estimations_bin.dta"' , keepusing(claim_cost claim_cost_se line_cost) nogen 
replace claim_cost = . if claim_cost_se == .

gen extra_line = (-n_S+1)
replace extra_line = 0 if n_S == .
replace claim_cost = 0 if n_S == .

gen indirect_cost = exp(claim_cost +(extra_line*line_cost)+continuation)
gcollapse (sum) indirect_cost*, by(visit_billing_id)
replace indirect_cost = ln(indirect_cost) + 0.5772156649

save `"$output/temp/relax_SUF_cont_value_model_merged.dta"', replace 


//merge into visit level data 
use `"$output/data/$line_file"', clear 

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"

// generate denial indicator 
egen final_paid = rowtotal(Y_?)
gen initial_denial = 1-Y_0
gen final_denial = 1-final_paid
bys visit_billing_id: gegen visit_initial_den = max(initial_denial)
bys visit_billing_id: gegen visit_final_den = max(final_denial)

// generate pi
bys visit_billing_id: gegen pi = sum(line_item_value)
assert pi >=0

// generate final denied amt 
egen Y_max = rowmax(Y_?)
assert Y_max == 0 | Y_max == 1
bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_max ==0
bys visit_billing_id: gegen denied_amt_final = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt_final = 0 if denied_amt_final == . 


// generate initial denied amt 
bys visit_billing_id: gegen denied_visit = min(Y_0)
replace denied_visit = 1-denied_visit

bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_0 ==0
bys visit_billing_id: gegen denied_amt = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt = 0 if denied_amt == . 

// generate RVU proxy of visit 
bys visit_billing_id: gegen RVU = sum(meanallowedMFFS) 

forvalues i = 1/5 {
	
	decode reason_`i', gen(reason_`i'_string)
	gen fineline_`i' = inlist(reason_`i'_string,"","45","1","2","3","253","223","23")
}
cap drop goodobs
egen allfine = rsum(fineline_?)
gen goodobs=allfine==5

bys visit_billing_id : gegen resubmitted = max(R_1)

// switch to visit level 
bys visit_billing_id: keep if _n == 1 
merge 1:1 visit_billing_id using `"$output/temp/relax_SUF_cont_value_model_merged.dta"'
replace indirect_cost = 0 if _merge == 1
drop _merge 

gen CIP = denied_amt - indirect_cost

*****************************************************
*****************************************************
**** DROP NEGATIVE CIP ***
*****************************************************
*****************************************************
sum CIP, d 

drop if CIP < 0 | CIP>r(p99)  //dropping outlier



*****************************************
***** Generate Regression Variables *****
*****************************************
gen ln_pi = ln(pi)	// define allowed amount for visit j as pi

// generate tau and lost revenue 
gen tau = CIP/pi
gen tau_rf = (denied_amt_final/pi)
rename denied_amt_final lost_revenue
rename denied_amt initial_denied_amt

lab var lost_revenue "Final denied amt for a visit"
lab var tau_rf "Tau calculation for each visit if zero resubmission cost"
lab var tau "Tau calculation for each visit"
lab var initial_denied_amt "Amt that is initially denied"
lab var visit_initial_den "At least one line item initially denied"
lab var visit_final_den "At least one line item finally denied"
lab var CIP "CIP at visit level"


//SMALL GROUPS 
// summarize tau 
reg tau if pay_type_pooled ==1 & size_indicator ==1
est save `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_smallgroups.ster"', replace

reg tau if pay_type_pooled ==2 & size_indicator ==1
est save `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_smallgroups.ster"', replace

reg tau if pay_type_pooled ==3 & size_indicator ==1
est save `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_smallgroups.ster"', replace

// summarize CIP 
reg CIP if pay_type_pooled ==1 & size_indicator ==1
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Medicare_byreasons_smallgroups.ster"', replace

reg CIP if pay_type_pooled ==2 & size_indicator ==1
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Medicaid_byreasons_smallgroups.ster"', replace

reg CIP if pay_type_pooled ==3 & size_indicator ==1
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Commercial_byreasons_smallgroups.ster"', replace

//LARGE GROUPS 
// summarize tau 
reg tau if pay_type_pooled ==1 & size_indicator ==2
est save `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_largegroups.ster"', replace

reg tau if pay_type_pooled ==2 & size_indicator ==2
est save `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_largegroups.ster"', replace

reg tau if pay_type_pooled ==3 & size_indicator ==2
est save `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_largegroups.ster"', replace

// summarize CIP 
reg CIP if pay_type_pooled ==1 & size_indicator ==2
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Medicare_byreasons_largegroups.ster"', replace

reg CIP if pay_type_pooled ==2 & size_indicator ==2
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Medicaid_byreasons_largegroups.ster"', replace

reg CIP if pay_type_pooled ==3 & size_indicator ==2
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Commercial_byreasons_largegroups.ster"', replace


erase `"$output/temp/relax_SUF_cont_value_model_merged.dta"'



****************************************************************
****************************************************************
*********** CIP calculation No Size Distinction ****************
****************************************************************
****************************************************************
use  `"$output/temp/relax_SUF_estimation_data_bin.dta"', clear

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



//merge in model estimates 
merge m:1 visit_code_cat state pay_type_pooled using `"$output/model_estimation/relax_SUF/estimations_nosize_bin.dta"' , keepusing(claim_cost claim_cost_se line_cost) nogen 
replace claim_cost = . if claim_cost_se == .

gen extra_line = (-n_S+1)
replace extra_line = 0 if n_S == .
replace claim_cost = 0 if n_S == .

gen indirect_cost = exp(claim_cost +(extra_line*line_cost)+continuation)
gcollapse (sum) indirect_cost*, by(visit_billing_id)
replace indirect_cost = ln(indirect_cost) + 0.5772156649

save `"$output/temp/relax_SUF_cont_value_model_merged.dta"', replace 


//merge into visit level data 
use `"$output/data/$line_file"', clear 

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"

// generate denial indicator 
egen final_paid = rowtotal(Y_?)
gen initial_denial = 1-Y_0
gen final_denial = 1-final_paid
bys visit_billing_id: gegen visit_initial_den = max(initial_denial)
bys visit_billing_id: gegen visit_final_den = max(final_denial)

// generate pi
bys visit_billing_id: gegen pi = sum(line_item_value)
assert pi >=0

// generate final denied amt 
egen Y_max = rowmax(Y_?)
assert Y_max == 0 | Y_max == 1
bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_max ==0
bys visit_billing_id: gegen denied_amt_final = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt_final = 0 if denied_amt_final == . 


// generate initial denied amt 
bys visit_billing_id: gegen denied_visit = min(Y_0)
replace denied_visit = 1-denied_visit

bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_0 ==0
bys visit_billing_id: gegen denied_amt = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt = 0 if denied_amt == . 

// generate RVU proxy of visit 
bys visit_billing_id: gegen RVU = sum(meanallowedMFFS) 

forvalues i = 1/5 {
	
	decode reason_`i', gen(reason_`i'_string)
	gen fineline_`i' = inlist(reason_`i'_string,"","45","1","2","3","253","223","23")
}
cap drop goodobs
egen allfine = rsum(fineline_?)
gen goodobs=allfine==5

bys visit_billing_id : gegen resubmitted = max(R_1)

// switch to visit level 
bys visit_billing_id: keep if _n == 1 
merge 1:1 visit_billing_id using `"$output/temp/relax_SUF_cont_value_model_merged.dta"'
replace indirect_cost = 0 if _merge == 1
drop _merge 

gen CIP = denied_amt - indirect_cost

*****************************************************
*****************************************************
**** DROP NEGATIVE CIP ***
*****************************************************
*****************************************************
sum CIP, d 

drop if CIP < 0 | CIP>r(p99)  //dropping outlier



*****************************************
***** Generate Regression Variables *****
*****************************************
gen ln_pi = ln(pi)	// define allowed amount for visit j as pi

// generate tau and lost revenue 
gen tau = CIP/pi
gen tau_rf = (denied_amt_final/pi)
rename denied_amt_final lost_revenue
rename denied_amt initial_denied_amt

lab var lost_revenue "Final denied amt for a visit"
lab var tau_rf "Tau calculation for each visit if zero resubmission cost"
lab var tau "Tau calculation for each visit"
lab var initial_denied_amt "Amt that is initially denied"
lab var visit_initial_den "At least one line item initially denied"
lab var visit_final_den "At least one line item finally denied"
lab var CIP "CIP at visit level"


// summarize tau 
reg tau if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_allgroups.ster"', replace

reg tau if pay_type_pooled ==2
est save `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_allgroups.ster"', replace

reg tau if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_allgroups.ster"', replace

// summarize CIP 
reg CIP if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Medicare_byreasons_allgroups.ster"', replace

reg CIP if pay_type_pooled ==2
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Medicaid_byreasons_allgroups.ster"', replace

reg CIP if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Commercial_byreasons_allgroups.ster"', replace

erase `"$output/temp/relax_SUF_cont_value_model_merged.dta"'


****************************************************************
****************************************************************
******* CIP calculation No Size and Reason Distinction *********
****************************************************************
****************************************************************
use  `"$output/temp/relax_SUF_estimation_data_bin.dta"', clear


** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



//merge in model estimates 
merge m:1 state pay_type_pooled using `"$output/model_estimation/relax_SUF/estimations_nosize_noreason_bin.dta"' , keepusing(claim_cost claim_cost_se line_cost) nogen 
replace claim_cost = . if claim_cost_se == .

gen extra_line = (-n_S+1)
replace extra_line = 0 if n_S == .
replace claim_cost = 0 if n_S == .

gen indirect_cost = exp(claim_cost +(extra_line*line_cost)+continuation)
gcollapse (sum) indirect_cost*, by(visit_billing_id)
replace indirect_cost = ln(indirect_cost) + 0.5772156649

save `"$output/temp/relax_SUF_cont_value_model_merged.dta"', replace 


//merge into visit level data 
use `"$output/data/$line_file"', clear 

// generate denial indicator 
egen final_paid = rowtotal(Y_?)
gen initial_denial = 1-Y_0
gen final_denial = 1-final_paid
bys visit_billing_id: gegen visit_initial_den = max(initial_denial)
bys visit_billing_id: gegen visit_final_den = max(final_denial)

// generate pi
bys visit_billing_id: gegen pi = sum(line_item_value)
assert pi >=0

// generate final denied amt 
egen Y_max = rowmax(Y_?)
assert Y_max == 0 | Y_max == 1
bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_max ==0
bys visit_billing_id: gegen denied_amt_final = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt_final = 0 if denied_amt_final == . 


// generate initial denied amt 
bys visit_billing_id: gegen denied_visit = min(Y_0)
replace denied_visit = 1-denied_visit

bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_0 ==0
bys visit_billing_id: gegen denied_amt = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt = 0 if denied_amt == . 

// generate RVU proxy of visit 
bys visit_billing_id: gegen RVU = sum(meanallowedMFFS) 

forvalues i = 1/5 {
	
	decode reason_`i', gen(reason_`i'_string)
	gen fineline_`i' = inlist(reason_`i'_string,"","45","1","2","3","253","223","23")
}
cap drop goodobs
egen allfine = rsum(fineline_?)
gen goodobs=allfine==5

bys visit_billing_id : gegen resubmitted = max(R_1)

// switch to visit level 
bys visit_billing_id: keep if _n == 1 
merge 1:1 visit_billing_id using `"$output/temp/relax_SUF_cont_value_model_merged.dta"'
replace indirect_cost = 0 if _merge == 1
drop _merge 

gen CIP = denied_amt - indirect_cost

*****************************************************
*****************************************************
**** DROP NEGATIVE CIP ***
*****************************************************
*****************************************************
sum CIP, d 

drop if CIP < 0 | CIP>r(p99)  //dropping outlier



*****************************************
***** Generate Regression Variables *****
*****************************************
gen ln_pi = ln(pi)	// define allowed amount for visit j as pi

// generate tau CIP
gen ln_one_tau = ln(1-(CIP/pi)) //define 1-CIP/pi as one_tau
gen one_tau = 1-(CIP/pi) //define 1-CIP/pi as one_tau

//generate tau CIP without the model (reduced form only)
gen ln_one_tau_rf = ln(1-(denied_amt_final/pi)) //define 1-CIP/pi as one_tau (only using final denied amt here)
gen one_tau_rf = 1-(denied_amt_final/pi) //define 1-CIP/pi as one_tau (only using final denied amt here)

// generate tau and lost revenue 
gen tau = CIP/pi
gen tau_rf = (denied_amt_final/pi)
rename denied_amt_final lost_revenue
rename denied_amt initial_denied_amt

lab var lost_revenue "Final denied amt for a visit"
lab var tau_rf "Tau calculation for each visit if zero resubmission cost"
lab var tau "Tau calculation for each visit"
lab var initial_denied_amt "Amt that is initially denied"
lab var visit_initial_den "At least one line item initially denied"
lab var visit_final_den "At least one line item finally denied"
lab var CIP "CIP at visit level"

// summarize tau 
reg tau if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_pooledreasons.ster"', replace

reg tau if pay_type_pooled ==2
est save `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_pooledreasons.ster"', replace

reg tau if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_pooledreasons.ster"', replace

// summarize tau without resubmission costs 
reg tau_rf if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_SUF/parameters/taurf_Medicare.ster"', replace

reg tau_rf if pay_type_pooled ==2
est save `"$output/model_estimation/relax_SUF/parameters/taurf_Medicaid.ster"', replace

reg tau_rf if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_SUF/parameters/taurf_Commercial.ster"', replace


// summarize CIP 
reg CIP if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Medicare_pooledreasons.ster"', replace

reg CIP if pay_type_pooled ==2
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Medicaid_pooledreasons.ster"', replace

reg CIP if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_SUF/parameters/CIP_Commercial_pooledreasons.ster"', replace

// summarize tau without resubmission costs 
reg lost_revenue if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_SUF/parameters/lost_revenue_Medicare.ster"', replace

reg lost_revenue if pay_type_pooled ==2
est save `"$output/model_estimation/relax_SUF/parameters/lost_revenue_Medicaid.ster"', replace

reg lost_revenue if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_SUF/parameters/lost_revenue_Commercial.ster"', replace


erase `"$output/temp/relax_SUF_cont_value_model_merged.dta"'


log close 
